package com.example.game;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.game.GamePage.databaseHelper;
import static com.example.game.GamePage.player;

public class setBuildingFragment extends Fragment {

    final int REFRESH = 0;
    final int CONTENT_COUNT = 10;
    final List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>(); //存储数据的数组列表

    private ImageButton mLeftMenu;
    int[] image_expense = new int[]{R.drawable.building1, R.drawable.building2 , R.drawable.building3, R.drawable.building4, R.drawable.building5, R.drawable.building6, R.drawable.building7, R.drawable.building8, R.drawable.building9, R.drawable.building10}; //存储图片

    String[][] content =new String[CONTENT_COUNT][5];           //前一位表示总共有几个项目，后一个表示每个项目(每个建筑)一共有几行


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        Log.d("TAG_building_list", "onCreateView: building_page");
        final View view = inflater.inflate(R.layout.building_fragment_layout, container, false);
        final SimpleAdapter adapter = setUpAdapter();
        final ListView listView = (ListView)view.findViewById(R.id.Building_ListView);
        listView.setAdapter(adapter);

        final Handler handler_refresh = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if(msg.what == REFRESH){

                    adapter.notifyDataSetChanged();
                    Log.d("TAG", "handleMessage: notifydatasetchanged ");
                }
            }
        };

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                String TAG = "TAG建筑页面";
                Log.d(TAG, "onListViewClick: Clicked " + Integer.toString(position));

                if( player.cash >= player.building_list[1][position]) {
                    Log.d(TAG, "onClick: checkpoint1");
                    player.building_list[0][position] += 1;
                    player.cash -= player.building_list[1][position];
                    player.building_list[1][position] *=1.15;
                    player.refresh_production();
                    setContent();
                    put_adapter_content();


                    Message msg = new Message();
                    msg.what = REFRESH;
                    handler_refresh.sendMessage(msg);

                    databaseHelper.saveGame();
                }
            }
        });

        return view;
    }
    public void put_adapter_content(){
        listitem.clear();
        for(int i=0; i< CONTENT_COUNT; i++)
        {
            if (i > 0 && player.building_list[0][i - 1] == 0) {
                break;
            }

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", image_expense[i]);
            for(int j = 0 ; j < 5 ; j ++ ) {
                map.put("content" + j, content[i][j]);
            }
            listitem.add(map);
        }
    }
    SimpleAdapter setUpAdapter()
    {
        String TAG = "TAG_setContent";
        setContent();
        put_adapter_content();



        //创建适配器
        // 第一个参数是上下文对象
        // 第二个是listitem
        // 第三个是指定每个列表项的布局文件
        // 第四个是指定Map对象中定义的两个键（这里通过字符串数组来指定）
        // 第五个是用于指定在布局文件中定义的id（也是用数组来指定）
        SimpleAdapter adapter = new SimpleAdapter(getActivity()
                , listitem
                , R.layout.building_fragment_item
                , new String[]{"image","content0","content1","content2","content3","content4"}
                , new int[]{R.id.Building_ImageView, R.id.Building_Row1, R.id.Building_Row2, R.id.Building_Row3, R.id.Building_Row4, R.id.Building_Row5});
        Log.d(TAG, "adapter settled");
        return adapter;
    }

    public void setContent(){
        String TAG = "TAG_setContent";
        for (int i = 0; i < CONTENT_COUNT ; i ++ ){
            content[i][0] = player.building_name[i] + " / 产量+" + player.convertLongToString((long)(player.getBuilding_orignal_production[i]*20));
            content[i][1] =" 当前数量 : " + player.convertLongToString(player.building_list[0][i]) + " / " + " 价格 : " + player.convertLongToString(player.building_list[1][i]);
            content[i][2] =" 总计秒产量 ： " + player.convertLongToString((long) (player.building_list[0][i] * player.getBuilding_orignal_production[i] * 20));
            if(player.building_list[0][i] != 0) {
                content[i][3] = " 秒产量占比 ： " + String.format("%.2f",((double) player.building_list[0][i] * 100.0 * player.getBuilding_orignal_production[i] / player.production)) + " %";
            }else{
                content[i][3] = " 秒产量占比 ： 0 %";
            }
            content[i][4] = player.builidng_background[i];
            Log.d(TAG, "set content : " + content[i][3]);
        }
        Log.d(TAG, "all content settled");
    }
}


